<?php

namespace Home\Controller;

require './vendor/autoload.php';

class WechatCcbController{

    private $receptionist = "c8819,c8822,c8815,c8825,c8821,c8801,c8820,c8824,c8826,C8707,c8818,c8816,c8803,c8804,c8817,c8808,c8813,c8814,c8911,c8807,c8809,c8802,c8805,c8811,c8812,maxwell.shi";
    //private $userIDArray = array(196,188,186,185,182,174,172,151,149,146,140,113,112,111,110,109,108,107,106,105,104,103,102,101,100,78);
    private $userIDArray = array(113,101,100,78);

    public function __construct(){
    }

    public function reciveContent(){

        // 获取数据
        $postData = @file_get_contents('php://input');

        // 将数据存放在中间文件
        //$file = "record.dat";
        //file_put_contents($file,$postData);

        // 将数据写入数据库
        // 解析数据
        $xml = new \DOMDocument();
        $xml->loadXML($postData);
        $fromUserName = $xml->getElementsByTagName('FromUserName');
        $trueFromUserName = $fromUserName->item(0)->nodeValue;

        if($trueFromUserName) {

            $createTime = $xml->getElementsByTagName('CreateTime');
            $trueCreateTime = $createTime->item(0)->nodeValue;

            $msgType = $xml->getElementsByTagName('MsgType');
            $trueMsgType = $msgType->item(0)->nodeValue;

            $trueContent = '';
            switch ($trueMsgType) {
                case 'text':
                    $content = $xml->getElementsByTagName('Content');
                    $trueContent = $content->item(0)->nodeValue;
                    break;
                case 'image':
                    $content = $xml->getElementsByTagName('PicUrl');
                    $trueContent = $content->item(0)->nodeValue;
                    break;
                case 'voice':
                    $content = $xml->getElementsByTagName('MediaId');
                    $trueContent = $content->item(0)->nodeValue;
                    break;
                case 'event':
                    $content = $xml->getElementsByTagName('EventKey');
                    $trueContent = $content->item(0)->nodeValue;
                    break;
                default:
                    break;

            }
            $saveData = array(
                'from_user_name' => $trueFromUserName,
                'create_time' => date('Y-m-d H:i:s', $trueCreateTime),
                'msg_type' => $trueMsgType,
                'msg_content' => $trueContent,
                'receptionist' => $this->receptionist
            );

            $wechatBbcLogModel = M('wechatCcbLog');
            $result = $wechatBbcLogModel->add($saveData);

            if ($result) {

                // push message
                $pushMsgData = array(
                    'title' => 'CCB龙卡微信',
                    'description' => $trueContent,
                    'action' => ''
                );
                foreach ($this->userIDArray as $key => $value) {
                    send_push($pushMsgData, $value);
                }

                echo "Success";
            } else {
                echo "False";
            }

        }else{
            echo "False";
        }

    }

    public function postData(){

        $xmlString = "<xml><ToUserName><![CDATA[gh_c9f9be1ceed9]]></ToUserName>
<FromUserName><![CDATA[ofO-ouEsCGEREFKWdQd-wV1H4szA]]></FromUserName>
<CreateTime>".time()."</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[good morning]]></Content>
<MsgId>6518886300010729202</MsgId>
</xml>";

//        $xmlString = "<xml><ToUserName><![CDATA[gh_c9f9be1ceed9]]></ToUserName>
//<FromUserName><![CDATA[ofO-ouEsCGEREFKWdQd-wV1H4szA]]></FromUserName>
//<CreateTime>1517796727</CreateTime>
//<MsgType><![CDATA[image]]></MsgType>
//<PicUrl><![CDATA[http://mmbiz.qpic.cn/mmbiz_jpg/CGa9O2F5JD6mBcXZ6KTMXmbcO3IvRO6$]]></PicUrl>
//<MsgId>6518887305033076574</MsgId>
//<MediaId><![CDATA[8akpz9LvIwzPbrTmJWGibG01QInci_aBQnbIlrHRd-xB2LGP3tiYg5xn18EDL$]]></MediaId></xml>";

//        $xmlString = "<xml><ToUserName><![CDATA[gh_c9f9be1ceed9]]></ToUserName>
//<FromUserName><![CDATA[ofO-ouEsCGEREFKWdQd-wV1H4szA]]></FromUserName>
//<CreateTime>1517796786</CreateTime>
//<MsgType><![CDATA[voice]]></MsgType>
//<MediaId><![CDATA[kR9ljctx97SURZ088JFPjADa489EjfbMXbBIvdKGTHOaVFdIUIntBNr2fdWrU$]]></MediaId>
//            <Format><![CDATA[amr]]></Format>
//<MsgId>6518887558436147067</MsgId>
//<Recognition><![CDATA[]]></Recognition>
//</xml>";

        $options = array(
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => 10,
            CURLOPT_TIMEOUT => 10,
            CURLOPT_SSL_VERIFYHOST => false,
            CURLOPT_SSL_VERIFYPEER => false,
        );

        $url = "http://gtoa.dd:8083/WechatCcb/reciveContent/";

        $data = $this->http($url,$xmlString,'POST');
        var_dump($data);

    }

    function http($url, $params, $method = 'GET', $header = array(), $multi = false) {
        $opts = array(CURLOPT_TIMEOUT => 30, CURLOPT_RETURNTRANSFER => 1, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_HTTPHEADER => $header);

        /* 根据请求类型设置特定参数 */
        switch(strtoupper($method)) {
            case 'GET' :
                $opts[CURLOPT_URL] = $url . '?' . str_replace("&amp;", "&", http_build_query($params));
                break;
            case 'POST' :
                //判断是否传输文件
                //$params = $multi ? $params : http_build_query($params);
                $opts[CURLOPT_URL] = $url;
                $opts[CURLOPT_POST] = 1;
                $opts[CURLOPT_POSTFIELDS] = $params;
                break;
            default :
                throw new Exception('不支持的请求方式！');
        }

        /* 初始化并执行curl请求 */
        $ch = curl_init();
        curl_setopt_array($ch, $opts);
        $data = curl_exec($ch);
        $error = curl_error($ch);
        curl_close($ch);
        if ($error)
            throw new Exception('请求发生错误：' . $error);
        return $data;
    }

}